<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$return['setting']=$this->plugin_setting('basic');
if($_RQ['op']=='main' || $_RQ['op']=='wait'){
	$return['params']=array('status'=>intval($_RQ['status']));
	$return['list']=array();
	exi($return);
}
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_farmer_gdopt',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		foreach($list as &$item){
			$item['statustitle']=$this->gdopt_status[$item['status']];
			$item['shoutypetitle']=$this->send_types[$item['shoutype']];
			$item['animal']=pdo_get('jy_farmer_guoshu',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['guoshu_id']),array('down_send_type0','down_send_type1','image','title','classify_id','growtime','shouyue','shoukg','shoufee','oneprice'));
			$item['animal']['image']=tomedia($item['animal']['image']);
			$item['animal']['atitle']=pdo_getcolumn('jy_farmer_gclassify',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['animal']['classify_id']),'title');
			if($item['number']<10){
				$item['number']='000'.$item['number'];
			}elseif($item['number']<100 && $item['number']>9){
				$item['number']='00'.$item['number'];
			}else{
				$item['number']='0'.$item['number'];
			}
			$item['order']=pdo_get('jy_farmer_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['order_id']),array('address','total','ordersn','money','createtime'));
			$item['animal']['shoukg']=$item['animal']['shoukg']*$item['order']['total'];
			$item['animal']['shoufee']=$item['animal']['shoufee']*$item['order']['total'];
			$totype=pdo_getcolumn('jy_farmer_worker_log',array('createtime >'=>$daytime,'pid'=>$item['id'],'totype'=>0,'type'=>0,'uniacid'=>$_SESSION['uniacid']),'id');
			if($totype){
				$item['totype0']=1;
			}
			$totype=pdo_getcolumn('jy_farmer_worker_log',array('createtime >'=>$daytime,'pid'=>$item['id'],'totype'=>1,'type'=>0,'uniacid'=>$_SESSION['uniacid']),'id');
			if($totype){
				$item['totype1']=1;
			}
			$totype=pdo_getcolumn('jy_farmer_worker_log',array('createtime >'=>$daytime,'pid'=>$item['id'],'totype'=>2,'type'=>0,'uniacid'=>$_SESSION['uniacid']),'id');
			if($totype){
				$item['totype2']=1;
			}
			$totype=pdo_getcolumn('jy_farmer_worker_log',array('createtime >'=>$daytime,'pid'=>$item['id'],'totype'=>3,'type'=>0,'uniacid'=>$_SESSION['uniacid']),'id');
			if($totype){
				$item['totype3']=1;
			}
			if($item['animal']['shouyue']==$month && ($item['status']==1 || $item['status']==2)){
				$item['isshou']=1;
			}
			$shouday=$item['order']['createtime']+$item['animal']['growtime']*24*3600;
			if($shouday<time() && ($item['status']==1 || $item['status']==2)){
				$item['isshou']=1;
			}
			$shouday=date('Y-m-d',$shouday);
			$item['animal']['shouday']=$shouday;
			if($item['status']==4){
				$item['shoufee']=$item['animal']['oneprice']*$item['shoukg'];
			}
			$item['imgs']=$item['imgs']?json_decode($item['imgs'],true):array();
			foreach($item['imgs'] as &$img){
				$img=tomedia($img);
			}
		}
	}
	$return['selectstore']=$return['setting']['stores'][0]?:array();
	$post=array('sendtype'=>$sendtype,'total'=>1,'address'=>array('pcd'=>array('city'=>'深圳市')),'peisongfee'=>$return['setting']['peisongfee']);
	//刷新运费
	$up_dispatch=model('jy_farmer/dispatch')->up_dispatch($post);
	$post=$up_dispatch['post']?:$post;
	$post['peisongfee']=$post['dispatchprice']?:$post['peisongfee'];
	$address=pdo_get('core_address',array('isdel'=>0,'isdefault'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if(!$address){
		$address=pdo_get('core_address',array('isdel'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	}
	$address['pcd']=json_decode($address['pcd'],true);
	$post['address']=$address;
	$post['addressid']=$address['id'];
	$return['post']=$post;
	$return['address']=$post['address'];
	$order=pdo_get('jy_farmer_order',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('username','mobile'),'id desc');
	$return['post']['username']=$order['username'];
	$return['post']['mobile']=$order['mobile'];
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='adopt'){
	if($_RQ['id']){
		exi('将立即通知管理员浇水！');
	}else{
		exi('该果树不存在，请重新选择','error');
	}
}
if($_RQ['op']=='weed'){
	$plant_id=$_RQ['id'];
	exi('将立即通知管理员除虫');	
}
if($_RQ['op']=='worm'){
	$plant_id=$_RQ['id'];
	exi('将立即通知管理员除草');	
}
if($_RQ['op']=='shifei'){
	$plant_id=$_RQ['id'];
	exi('将立即通知管理员施肥');	
}
if($_RQ['op']=='shou1'){
	if($_RQ['id']){
		model('jy_farmer/order')->order_shou1($_RQ['id'],0);
		exi('提交申请成功，请等待审核！');
	}else{
		exi('该果树不存在，请重新选择','error');
	}
}
